import common.parser.JavaClass;

import java.io.*;


public class classAnalyze {
	private  String currentMethod="";
	private  int readVarCount = 0;
	private  int writeVarCount = 0;
	private JavaClass javaclass = null;
	private String[] methods = null;
	
	public classAnalyze(InputStream data) {		
			if(data!=null) {				
				//Klasse lesen
				this.javaclass = new JavaClass(data);	
				this.setReadVarCount(0);
				this.setWriteVarCount(0);
			}
	}
	public void read() throws Exception {
		if(this.javaclass!=null) {
				this.javaclass.readClass();		
				setMethods(this.javaclass.getMethods());
		}
	}
	public int analyzeCode() {
		if(this.currentMethod.length()!=0 && this.javaclass!=null) {
			int methodLength = this.javaclass.getMethodLength(this.currentMethod);
			
			//Jede Instruction in der Methode wird durchgegangen und auf lesenden oder schreibenden Zugriff von Integer-Variablen untersucht.
			for (int instrNo = 0; instrNo < methodLength; instrNo++) {
				
				int instrOpcode = this.javaclass.getInstructionOpcode(this.currentMethod, instrNo);
				
				if(instrOpcode==21 || (instrOpcode>=26 && instrOpcode<=29)) { // lokale Integer-Variable wird gelesen
					this.readVarCount++;
				}
				if(instrOpcode==54 || (instrOpcode>=59 && instrOpcode<=62)) { // lokale Integer-Variable wird geschrieben
					this.writeVarCount++;
				}
			}
			return 1;
		}
		else {
			return 0;
		}
	}
	private void setReadVarCount(int param) {
		this.readVarCount=param;
	}
	private void setWriteVarCount(int param) {
		this.writeVarCount=param;
	}
	
	public int getReadVarCount() {
		return this.readVarCount;
	}
	
	public int getWriteVarCount() {
		return this.writeVarCount;
	}
	public String[] getMethods() {
		return this.methods;
	}
	public void setMethods(String[] param) {
		this.methods = param;
	}
	
	public int setReadMethod(String methodname) {
		//Überprüfung der Methoden der Klasse auf die gewünschte Methode des Argumentes
		int result = 0;
		for(int i = 0; i < this.methods.length; i++) {
			if(this.methods[i].startsWith(methodname)) {
				this.currentMethod = this.methods[i];
				result = 1;
			}
		}
		return result;
	}
}





